<style scoped>
    .buttonLogin {
        margin-left: 20px;
        margin-top: 10px;
        width: calc(100% - 40px);

    }

    .pLogin {
        margin-top: 10px;
        color: #999;
    }
</style>

<template>
    <div style="padding: 10px;background-color: #dfe2e5;">

        <van-cell-group>
            <van-field
                    v-model="userId"
                    required
                    clearable
                    label="用户名"
                    icon="fail"
                    placeholder="请输入用户名"
                    @click-icon="weChatRegister"
            />

            <van-field
                    v-model="password"
                    type="password"
                    label="密码"
                    placeholder="请输入密码"
                    required
            />
        </van-cell-group>
        <p class="pLogin">
            <van-icon name="fail" style="color: #ff113a"/>
            温馨提示：用户不存在会跳转至注册界面
        </p>
        <van-button type="primary" size="normal" class="buttonLogin" @click="weChatLogin">登录</van-button>
    </div>
</template>

<script>
    export default {
        name: "WeChatLogin",
        data() {
            return {
                userId: '',
                password: '',
                openId: ''
            }
        },
        methods: {
            weChatLogin() {
                let self = this;

                this.$http({
                    headers: {'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'},
                    transformRequest: [data => self.$qs.stringify(data)],
                    method: 'post',
                    url: '/core/weChatLogin',
                    data: {
                        userId: self.userId,
                        password: self.password,
                        openId: self.openId
                    }
                }).then(function (response) {
                    let data = self.common.handleDataNotify(response.data, self);
                    //   self.common.setStorageS("token", data.token);
                    self.common.setStorageS("userinfo", data);
                    self.$router.push({path: '/weChatIndex'});
                }).catch(function (error) {
                    self.$notify({
                        message: error.response.data.message,
                        duration: 3000
                    });

                    /*setTimeout(function () {
                        if("100011"===error.response.data.code){
                            self.$router.push({path: '/registerUser'});
                        }
                    },3000)*/


                });


            },
            weChatRegister() {
                let self = this;
                self.$router.push({
                    path: '/weChatRegister', params: {
                        openId: self.openId
                    }
                });

            }
        },
        mounted() {
            let self = this;
            self.$http.get('/core/wechatMp/getOpenId', {
                params: {
                    code: self.common.getUrlKey("code"),
                }
            }).then(function (response) {
                let data = self.common.handleDataNotify(response.data, self);
                if (undefined != data) {
                    self.common.setStorageS("token", data.token);
                    self.common.setStorageS("userinfo", data.userinfo);
                    self.common.setStorageS("wechatinfo", data.wechatinfo);
                    self.$router.push({path: '/weChatIndex'});
                }

            }).catch(function (error) {
                self.openId = error.response.data.data;
            });
        }
    }
</script>

